﻿Imports LRS.BLL
Imports LRS.DTO
Imports System.Windows.Forms
Imports System.Collections.Generic

Public Class ManageOrder
    Private Class OrderInfo

        Private _SalesOrderNumber As String
        Public Property SalesOrderNumber() As String
            Get
                Return _SalesOrderNumber
            End Get
            Set(ByVal value As String)
                _SalesOrderNumber = value
            End Set
        End Property


        Private _CustomerName As String
        Public Property CustomerName() As String
            Get
                Return _CustomerName
            End Get
            Set(ByVal value As String)
                _CustomerName = value
            End Set
        End Property


        Private _TotalAmount As Single
        Public Property TotalAmount() As Single
            Get
                Return _TotalAmount
            End Get
            Set(ByVal value As Single)
                _TotalAmount = value
            End Set
        End Property


        Private _DateOfBill As DateTime
        Public Property DateOfBill() As DateTime
            Get
                Return _DateOfBill
            End Get
            Set(ByVal value As DateTime)
                _DateOfBill = value
            End Set
        End Property


        Private _SalesOrderID As Integer
        Public Property SalesOrderID() As Integer
            Get
                Return _SalesOrderID
            End Get
            Set(ByVal value As Integer)
                _SalesOrderID = value
            End Set
        End Property

    End Class
    Private Class DetailInfo

        Private _ProductID As Integer
        Public Property ProductID() As Integer
            Get
                Return _ProductID
            End Get
            Set(ByVal value As Integer)
                _ProductID = value
            End Set
        End Property

        Private _ProductName As String
        Public Property ProductName() As String
            Get
                Return _ProductName
            End Get
            Set(ByVal value As String)
                _ProductName = value
            End Set
        End Property

        Private _Description As String
        Public Property Description() As String
            Get
                Return _Description
            End Get
            Set(ByVal value As String)
                _Description = value
            End Set
        End Property

        Private _TotalAmount As Single
        Public Property TotalAmount() As Single
            Get
                Return _TotalAmount
            End Get
            Set(ByVal value As Single)
                _TotalAmount = value
            End Set
        End Property

        Private _AddedFee As Single
        Public Property AddedFee() As Single
            Get
                Return _AddedFee
            End Get
            Set(ByVal value As Single)
                _AddedFee = value
            End Set
        End Property

        Private _Quantity As Integer
        Public Property Quantity() As Integer
            Get
                Return _Quantity
            End Get
            Set(ByVal value As Integer)
                _Quantity = value
            End Set
        End Property

    End Class
    Private Sub ManageOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gridOrder.AutoGenerateColumns = False
        gridProduct.AutoGenerateColumns = False
        GetData()
    End Sub
    Private Sub GetData(Optional ByVal IsSearching As Boolean = False, Optional ByVal IsOnHold As Boolean = False)
        oldRowIndex = -1
        Dim lst As List(Of OrderInfo) = New List(Of OrderInfo)
        Dim lstTemp As List(Of eSalesOrder)
        If (IsSearching) Then
            lstTemp = bSalesOrder.GetOrdersByKeyword(txtSearch.Text)
        ElseIf (IsOnHold) Then
            lstTemp = bSalesOrder.GetOrdersOnHold()
        Else
            lstTemp = bSalesOrder.GetOrders()
        End If

        For Each order As eSalesOrder In lstTemp
            Dim info As OrderInfo = New OrderInfo()
            info.SalesOrderNumber = order.SalesOrderNumber
            info.DateOfBill = order.DateOfBill
            Dim cust As eCustomer = bCustomer.GetCustomerByID(order.CustomerID)
            info.CustomerName = cust.LastName + " " + cust.FirstName
            info.TotalAmount = order.TotalAmount
            info.SalesOrderID = order.SalesOrderID
            lst.Add(info)
        Next

        gridOrder.DataSource = Nothing
        gridOrder.DataSource = lst
    End Sub
    Private oldRowIndex As Integer = -1
    Private currentOrder As eSalesOrder = Nothing
    Private Sub GetDetailData(ByVal SalesOrderID As Integer)
        Dim lst As List(Of DetailInfo) = New List(Of DetailInfo)
        Dim lstTemp = bSalesOrderDetail.GetSalesOrderDetailByOrderID(SalesOrderID)
        For Each item As eSalesOrderDetail In lstTemp
            Dim info As DetailInfo = New DetailInfo()
            info.ProductName = bProduct.GetProductByID(item.ProductID).ProductName
            info.Quantity = item.Quantity
            info.TotalAmount = item.TotalAmount
            info.Description = item.Description
            info.AddedFee = item.AddedFee
            info.ProductID = item.ProductID
            lst.Add(info)
        Next
        gridProduct.DataSource = Nothing
        gridProduct.DataSource = lst
    End Sub
    Private Sub gridOrder_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridOrder.RowEnter
        grpDetail.Enabled = True
        If (e.RowIndex = oldRowIndex) Then
            Return
        Else
            oldRowIndex = e.RowIndex
        End If
        Dim OrderID As Integer = gridOrder.Rows(e.RowIndex).Cells("SalesOrderID").Value
        Dim Order As eSalesOrder = bSalesOrder.GetOrderByID(OrderID)
        currentOrder = Order
        Dim cust As eCustomer = bCustomer.GetCustomerByID(Order.CustomerID)
        lblCustomerNumber.Text = cust.LastName + " " + cust.FirstName
        lblDateOfBill.Text = Order.DateOfBill.ToString("dd/MM/yyyy")
        lblDateOfReturn.Text = Order.DateOfReturn.ToString("dd/MM/yyyy")
        Dim emp As eEmployee
        emp = bEmployee.GetEmployeeByID(Order.EmployeeID)
        lblGiveEmployee.Text = emp.LastName + " " + emp.FirstName
        emp = bEmployee.GetEmployeeByID(Order.GetEmployeeID)
        If (emp Is Nothing) Then
            lblGetEmployee.Text = "Không có"
        Else
            lblGetEmployee.Text = emp.LastName + " " + emp.FirstName
        End If

        lblTotalAmount.Text = Order.TotalAmount.ToString("#,#")
        lblOrderNumber.Text = Order.SalesOrderNumber

        gridProduct.AutoGenerateColumns = False
        GetDetailData(Order.SalesOrderID)
        lblFine.Text = "0"
        If (Order.OnHold) Then
            btnConfirm.Visible = True
            lblStatus.Text = "Chưa hoàn trả"
            Dim overDue As Integer = DateTime.Now.Subtract(Order.DateOfReturn).Days
            If (overDue > 0) Then
                lblFine.Text = (Order.TotalAmount * LRSInfo.FineFactor * overDue).ToString("#,#")
            End If
            gridProduct.Columns("Description").ReadOnly = False
            gridProduct.Columns("AddedFee").ReadOnly = False
            lblActualDate.Text = ""
            labelFinalAmount.Visible = True
            lblFinalAmount.Visible = True
        Else
            btnConfirm.Visible = False
            lblStatus.Text = "Đã hoàn trả"
            gridProduct.Columns("Description").ReadOnly = True
            gridProduct.Columns("AddedFee").ReadOnly = True
            lblActualDate.Text = Order.ActualDateOfReturn.ToString("dd/MM/yyyy")
            lblFine.Text = Order.Fine
            labelFinalAmount.Visible = False
            lblFinalAmount.Visible = False
        End If
        UpdateFinalAmount()
    End Sub

    Private Sub UpdateFinalAmount()
        Dim fine As Single
        If (lblFine.Text.Length = 0) Then
            fine = 0
        Else
            fine = Single.Parse(lblFine.Text)
        End If

        Dim addedFeeSum As Single = 0

        For index = 0 To gridProduct.Rows.Count - 1
            Dim addedFee As Single = gridProduct.Rows(index).Cells("AddedFee").Value
            addedFeeSum = addedFeeSum + addedFee
        Next
        addedFeeSum += fine
        If (addedFeeSum = 0) Then
            lblFinalAmount.Text = "0"
        Else
            lblFinalAmount.Text = addedFeeSum.ToString("#,#")
        End If

    End Sub

    Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
        currentOrder.OnHold = False
        currentOrder.ActualDateOfReturn = DateTime.Now
        currentOrder.GetEmployeeID = LRSInfo.UserID
        currentOrder.Fine = Single.Parse(lblFinalAmount.Text)
        bSalesOrder.UpdateOrder(currentOrder)

        For index = 0 To gridProduct.Rows.Count - 1
            Dim detail As eSalesOrderDetail = New eSalesOrderDetail()
            detail.ProductID = gridProduct.Rows(index).Cells("ProductID").Value
            detail.Description = gridProduct.Rows(index).Cells("Description").Value
            detail.AddedFee = gridProduct.Rows(index).Cells("AddedFee").Value
            detail.SalesOrderID = currentOrder.SalesOrderID
            detail.Quantity = gridProduct.Rows(index).Cells("Quantity").Value
            detail.TotalAmount = gridProduct.Rows(index).Cells("TotalAmount").Value
            bSalesOrderDetail.Update(detail)
        Next
        grpDetail.Enabled = False
        Dim i = oldRowIndex
        oldRowIndex = -1
        GetData()
        gridOrder.Rows(i).Cells("SalesOrderNumber").Selected = True
        'gridOrder.Rows(i).Selected = True
    End Sub

    Private Sub gridProduct_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridProduct.CellEndEdit
        If (gridProduct.Columns(e.ColumnIndex).Name.Equals("AddedFee")) Then
            UpdateFinalAmount()
        End If
    End Sub

    Private Sub gridProduct_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles gridProduct.DataError
        MessageBox.Show("Bạn nhập dữ liệu không hợp lệ", "Thông báo")
        e.Cancel = True
    End Sub

    Private Sub btnDisplayAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayAll.Click
        GetData()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        GetData(True)
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnOnHoldSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOnHoldSearch.Click
        GetData(False, True)

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If (btnConfirm.Visible = True) Then
            MessageBox.Show("Bạn không thể xóa một hóa đơn chưa được hoàn trả")
            Return
        End If
        Dim result = MessageBox.Show("Bạn có chắc chắn muốn xóa hóa đơn này", "Xác nhận", MessageBoxButtons.YesNo)
        If (result = DialogResult.Yes) Then
            bSalesOrder.DeleteOrder(currentOrder.SalesOrderID)
            GetData()
        End If
    End Sub
End Class